<template>
    <div class="author">

    </div>
</template>
<script>

export default {
    name: 'error',
    data() {
        return {
            BaseUrl: 'https://open.weixin.qq.com/connect/oauth2/authorize?',
            tokenUrl: 'https://api.weixin.qq.com/sns/oauth2/access_token?',
            JsApiData: "",
            GetCodes: {
                //公众号的唯一标识
                AppId: this.CONSTANT.cofcoAppId, 
                //授权后重定向的回调链接地址(填当前页)
                GetCodes: this.CONSTANT.weChatBaseUrl,
                //返回类型，请填写code
                Response_type: "code",
                //应用授权作用域，snsapi_base （不弹出授权页面，直接跳转，只能获取用户openid）
                Scope: "snsapi_base",
                //重定向后会带上state参数，开发者可以填写a-zA-Z0-9的参数值，最多128字节
                State: "STATE",
                //必须带此参数
                Wechat_Redirect: "#wechat_redirect"
            },
            inMode: '',
            mobPhone: ''
        }
    },
    created: function() {
        this.$dialog.loading.open('请稍后');
        this.inMode = this.$store.state.inMode || '';
        this.mobPhone = this.$route.query.mobPhone || '';
        if (this.mobPhone != '') {
            this.$store.commit('mobPhone', this.mobPhone);
        }
        
        this.GetCode();
    },
    methods: {
        //拼接获取code的地址
        ReturnGetCodeUrl() {
            let appid = this.GetCodes.AppId;
            // 企业微信进入的场合,企业的CorpID 
            if (this.inMode == "qiye") {
                appid = this.CONSTANT.cofcoCorpId;
            }

            return this.BaseUrl + "appid=" + appid + "&redirect_uri="
                + encodeURIComponent(this.GetCodes.GetCodes) + "&response_type="
                + this.GetCodes.Response_type + "&scope=" + this.GetCodes.Scope + "&state="
                + this.GetCodes.State + this.GetCodes.Wechat_Redirect
        },
        //取localStorage数据
        GetStorage(name) {
            return localStorage.getItem(name);
        },
        //写localStorage数据
        SetStorage(name, value) {
            return localStorage.setItem(name, value);
        },
        //获取code
        GetCode() {
            //如果有code参数，那么GetOpenId获取openid
            if (this.GetQueryString("code")) {
                this.GetOpenId(this.GetQueryString("code"))
                //没有那么重定向去获取
            } else {
                //重定向去微信来获取code
                window.location = this.ReturnGetCodeUrl();
            }
        },
        //通过上面的GetCode()取得code，然后通过code取openid
        GetOpenId(code) {
            //判断本地localStorag是否已经有openid，有则不获取，没有就去获取
            this.$axios.get(this.CONSTANT.wapiBaseUrl + 'wxPay/getwxOpenId?code=' + code + '&inMode='+this.inMode)
                .then((response) => {
                    this.$dialog.loading.close();
                    if (response.status == 200) {
                        if (response.data.status == 1) {
                            this.$store.commit('openId', response.data.object);
                            if (this.$store.state.giftCode != '') {
                                this.$store.commit('routerTo', '/');
                                // this.$router.push('/receiveGifts');
                                this.$router.replace('/receiveGifts?mobPhone=' + this.$store.state.mobPhone);
                            } else {
                                let detailPage = this.$store.state.detailPage || '';
                                let routerTo = this.$store.state.routerTo || '';
                                // 微信退出 --> 微信登陆 -->进入商品详情 --> 返回 --> 通过首页进入签到 -->>立即签到
                                if (detailPage == undefined || detailPage == '') {
                                    // let routerTo = this.$store.state.routerTo;
                                    // 商品详情页面跳转过来时 routerTo= "/commodity?goodsCommonid=8"
                                    // 砸金蛋页面跳转过来时 routerTo= "/winegg?goodsCommonid=8"
                                    // 签到有礼页面跳转过来时 routerTo= "/signIn"
                                    // 体检预约页面跳转过来时 routerTo= "/appointment"
                                    if (routerTo.indexOf('commodity') != -1) {
                                        this.$router.push(routerTo);
                                    } else if (routerTo.indexOf('winegg') != -1 || 
                                                routerTo.indexOf('signIn') != -1 || 
                                                routerTo.indexOf('appointment') != -1) {
                                        this.$router.replace(routerTo);
                                    } else {
                                        // 防止首页返回到其他页面
                                        this.$router.replace('/');
                                    }
                                } else {
                                    // session超时 -->进入商品详情 --> 返回 --> 通过首页进入签到 -->>立即签到
                                    // 砸金蛋页面跳转过来时 routerTo= "/winegg?goodsCommonid=8"
                                    // 签到有礼页面跳转过来时 routerTo= "/signIn"
                                    // 体检预约页面跳转过来时 routerTo= "/appointment"
                                    if (routerTo.indexOf('winegg') != -1 || 
                                            routerTo.indexOf('signIn') != -1 || 
                                            routerTo.indexOf('appointment') != -1) {
                                        this.$router.replace(routerTo);
                                    } else {
                                        // 商品详情页面跳转过来时 detailPage= "/commodity?goodsCommonid=8"
                                        this.$router.push(detailPage);
                                    }
                                }
                            }
                        }
                    }
                }).catch((error) => {
                    this.$router.push('/');
                    this.$dialog.loading.close();
                });
        },
        //获取地址栏code参数
        GetQueryString(name) {
            var url = new RegExp("(^|&)" + name + "=([^&]*)(&|$)");
            var newUrl = window.location.search.substr(1).match(url);
            if (newUrl != null) {
                return unescape(newUrl[2]);
            } else {
                return false;
            }
        }
    }
}
</script>
<style  scoped>

</style>